package dev.ece.suit.hexmap;

import java.util.ArrayList;
import java.util.List;

import dev.ece.core.vertex.VAO;
import dev.ece.core.vertex.VBO;
import dev.ece.util.lib.Vector2f;
import dev.ece.util.lib.Vector3f;

public class MapCreviceVAO {

	private VBO verticeVBO;
	
	private VBO uvVBO;
	
	private VBO normalsVBO;
	
	private VBO alphaVBO;
	
	private VAO vao;
	
	public MapCreviceVAO(VBO verticeVBO, VBO uvVBO, VBO normalsVBO, VBO alphaVBO) {
		this.verticeVBO = verticeVBO;
		this.uvVBO = uvVBO;
		this.normalsVBO = normalsVBO;
		this.alphaVBO = alphaVBO;
	}
	
	public static MapCreviceVAO createMapCreviceVAO(TerrainModelCreator terrainModelCreator, MapCrevice[] crevices) {
		List<Vector3f> vertice = new ArrayList<Vector3f>();
		List<Vector2f> uv = new ArrayList<Vector2f>();
		List<Vector3f> normals = new ArrayList<Vector3f>();
		List<Float> alpha = new ArrayList<Float>();
		
		for(int i=0; i<crevices.length; i++) {
			terrainModelCreator.getMapCreviceCreator().fillData(crevices[i], vertice, uv, normals, alpha);
		}
		
		VBO verticeVBO = VBO.newInstance3f(vertice);
		VBO uvVBO = VBO.newInstance2f(uv);
		VBO normalsVBO = VBO.newInstance3f(normals);
		VBO alphaVBO = VBO.newInstance1f(alpha);
		
		vertice.clear();
		uv.clear();
		normals.clear();
		alpha.clear();
		
		vertice = null;
		uv = null;
		normals = null;
		alpha = null;
		return new MapCreviceVAO(verticeVBO, uvVBO, normalsVBO, alphaVBO);
	}
	
	public void initialize() {
		vao = new VAO();
		vao.bind();
		vao.bindVertexArrayBuffer(verticeVBO, 3);
		vao.bindVertexArrayBuffer(uvVBO, 2);
		vao.bindVertexArrayBuffer(normalsVBO, 3);
		vao.bindVertexArrayBuffer(alphaVBO, 1);
		vao.finish();
		
		verticeVBO = null;
		uvVBO = null;
		normalsVBO = null;
		alphaVBO = null;
	}
	
	public void destroy() {
		if(vao != null) {
			vao.destroy();
		}
	}
	
	public void bind() {
		vao.bind();
	}
	
}
